from datetime import datetime
from time import sleep, mktime
from utils.powerswitch import loca_wakeup_switch



def save_result(path, r):
    with open(path, encoding='utf8', mode='a') as file:
        file.write(str(datetime.now()) + ' ' + r + '\n')


# 计算 datetime时间差
def datetime_get_time_difference(t1, t2):
    return str(int(mktime(t1.timetuple())) - int(mktime(t2.timetuple())))


# 门锁 进入带配网状态
def lock_clean_ready(com, delay):
    try:
        # 清除配网信息
        com.exec(command=bytes.fromhex('AA 55 05 00 00 00 00 00 00 04'))
        sleep(15)
        loca_wakeup_switch(delay)
        sleep(5)
        # 进入待配网状态
        com.exec(command=bytes.fromhex('AA 55 01 16 09 03 01 01 00 24'))
    except:
        print('疑似门锁发送进入带配网状态指令失败')